const pup = require('puppeteer');
(async () => {
  const browser = await pup.launch({
    // 打开开发者工具, 当此值为true时, headless总为false
    //  devtools: true,
    // 关闭headless模式, 会打开浏览器
    // headless: false,
    defaultViewport: null //Defaults to an 800x600 viewport
  });
  const page = await browser.newPage();
  await page.goto('https://juejin.im/books');
  const dimensions = await page.evaluate(() => {
    // 在浏览器中执行函数，相当于在控制台中执行函数，返回一个 Promise
    // alert('evaluate')
    document.documentElement.scrollTop = 1
    return new Promise(resolve => {
      setTimeout(() => {
        resolve({
          width: document.documentElement.scrollWidth,
          height: document.documentElement.scrollHeight,
          deviceScaleFactor: window.devicePixelRatio
        })
      }, 2000)
    })
    // return {
    //   width:document.documentElement.scrollWidth,
    //   height:document.documentElement.scrollHeight,
    //   deviceScaleFactor:window.devicePixelRatio
    // }
  })
  // await page.type('.search-input','react',{delay:0});
  // await page.keyboard.press('Enter');
  // await page.waitFor(1000);
  console.log('dimensions:', JSON.stringify(dimensions, null, 2))
  await page.setViewport({ ...dimensions,
    width: 1920
  })
  await page.screenshot({
    path: '掘金小册.png'
  });
  await browser.close();
})()